{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 堆排序算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保持堆性质\n",
    "\n",
    "def Max_heapify(L, i):\n",
    "    '''\n",
    "    Insert ith node and maintain  max heap.\n",
    "    Input:\n",
    "    - L: A list. And the left and right subtree of i is max heap.\n",
    "    - i: the index.\n",
    "    \n",
    "    Output:\n",
    "    - L: A max heap.\n",
    "    '''\n",
    "    l = 2*i + 1\n",
    "    r = 2*i + 2\n",
    "    greatest_index = None\n",
    "    if l < len(L) and L[l] > L[i]:\n",
    "        greatest_index = l\n",
    "    else:\n",
    "        greatest_index = i\n",
    "    if r < len(L) and L[r] > L[greatest_index]:\n",
    "        greatest_index = r\n",
    "    if greatest_index != i:\n",
    "        L[i], L[greatest_index] = L[greatest_index], L[i]\n",
    "        Max_heapify(L, greatest_index)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[14, 8, 7, 2, 4, 1]\n"
     ]
    }
   ],
   "source": [
    "# run this to check Max_heapify\n",
    "L = [4, 14 ,7 ,2 ,8 ,1] # 从 0 开始\n",
    "Max_heapify(L,0)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 构建堆\n",
    "def Build_Max_Heap(L):\n",
    "    '''\n",
    "    Construct the max heap.\n",
    "    Input:\n",
    "    - L: A unordered list.\n",
    "    Output:\n",
    "    - L: A max heap.\n",
    "    '''\n",
    "    n = len(L)\n",
    "    for i in range(math.floor(n/2),-1,-1): # range 的区间是 [start, stop)\n",
    "        Max_heapify(L,i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[25, 13, 20, 8, 7, 17, 2, 5, 4]\n"
     ]
    }
   ],
   "source": [
    "# run this to check build max heap\n",
    "L = [5,13,2,25,7,17,20,8,4]\n",
    "Build_Max_Heap(L)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 堆排序\n",
    "def Heap_Sort(L):\n",
    "    Build_Max_Heap(L)\n",
    "    for i in range(len(L)-1,-1,-1):\n",
    "        L[0],L[i] = L[i],L[0] \n",
    "        t = L[0:i] # 区间是 [0, i)\n",
    "        Max_heapify(t,0)\n",
    "        L[0:i] = t\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 7, 8, 14]\n"
     ]
    }
   ],
   "source": [
    "L = [1,2,8,7,14,4]\n",
    "Heap_Sort(L)\n",
    "print(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
